package com.g7.dsp.forprotocol.contexts;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.g7.dsp.forprotocol.beans.SWTBean;

public class ApplicationContext {
	// For Singleton Object
	private static ApplicationContext app = null;
	private List<JPanel> panelList;
	private SWTBean swtBean;
	private String imgDirPath;

	// Constructors
	private ApplicationContext() {
		imgDirPath = System.getProperty("user.dir") + "\\imgs";
		panelList = new ArrayList<JPanel>();
		swtBean = new SWTBean();
	}

	public static ApplicationContext getApplicationContext() {
		if (app == null) {
			synchronized (ProtocolContext.class) {
				if (app == null) {
					app = new ApplicationContext();
					// 初始化HashMap
				}
			}
		}
		return app;
	}

	public List<JPanel> getPanelList() {
		return panelList;
	}

	public void setPanelList(List<JPanel> panelList) {
		this.panelList = panelList;
	}

	public SWTBean getSwtBean() {
		return swtBean;
	}

	public void setSwtBean(SWTBean swtBean) {
		this.swtBean = swtBean;
	}

	public String getImgDirPath() {
		return imgDirPath;
	}

	public void initUI(JFrame jf) {
		if (panelList.size() > 0) {
			for (int i = 0; i < panelList.size(); i++) {
				JPanel jp = panelList.get(i);
				if (jp != null) {
					jf.remove(jp);
					jp.removeAll();
					jp.repaint();
					jp = null;
				}
			}
			panelList.clear();
		}

		if (swtBean.getCanvas() != null) {
			jf.remove(swtBean.getCanvas());
			swtBean.setCanvas(null);
		}
	}
}
